<!DOCTYPE html>
<html lang="en-us" id="extr-page">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" media="screen"
	href="../../global/css/bootstrap.min.css">
<link rel="stylesheet"
	href="../../global/css/ztree/zTreeStyle/zTreeStyle.css" type="text/css">
<link rel="stylesheet" href="../../global/css/ztree/demo.css"
	type="text/css">
</head>
<body>
	<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
		<form class="form-horizontal" onsubmit="return false" id="form"
			enctype="multipart/form-data">
			<fieldset>
				<input type="hidden" id="id" name="id">
				<input type="hidden" id="goodsDetail" name="goodsDetail">
				<div class="form-group">
					<label class="col-md-2 control-label">礼包名称:</label>
					<div class="col-md-10">
						<input class="form-control" placeholder="名称" type="text"
							name="giftName" id="giftName" data-bv-notempty="true"
							data-bv-notempty-message="礼包名称不能为空">
					</div>
				</div>
				<div class="form-group">
					<label class="col-md-2 control-label">礼包内容:</label>
					<div class="col-md-10">
						<select class="form-control input-sm" id="goodsName" name="goodsName" onchange="goodsChange()">
							<option value="">请选择</option>
						</select>
						<a onclick="goodsDeatil()" style="text-decoration:none">商品详情</a>
					</div>
					
				</div>
				<div class="form-group">
					<label class="col-md-2 control-label">礼包价格:</label>
					<div class="col-md-10">
						<input type="text" class="form-control" placeholder="现价"
							name="zkPrice" id="zkPrice" />
					</div>
				</div>
				<div class="form-group">
					<label class="col-md-2 control-label">零钱:</label>
					<div class="col-md-10">
						<input type="text" class="form-control" placeholder="零钱"
							name="pocketMoney" id="pocketMoney" />
					</div>
					<div class="form-group">
						<label class="col-md-2 control-label">礼包主图:</label>
						<div class="col-md-10">
							<img id="picBanner1" style="width: 100px; height: 60px" /> <input
								type="file" name="picBanner1" id="picBanner1" />
						</div>
					</div>
					<div class="form-group">
						<label class="col-md-2 control-label">礼包轮播图:</label>
						<div class="col-md-10" id="picLb">
							<input type="file" multiple name="picLb1" id="picLb1" />
						</div>
					</div>
					<div class="form-group">
						<label class="col-md-2 control-label">礼包大图:</label>
						<div class="col-md-10">
							<img id="picBig1" style="width: 100px; height: 60px" /> <input
								type="file" name="picBig1" id="picBig1" />
						</div>
					</div>

				</div>

				<div class="form-group">
					<label class="col-md-2 control-label">购买前条件:</label>
					<div class="col-md-10" style="width: 120px">
						<select class="form-control input-sm" name="buyBefor" id="buyBefor">
							<option value="">请选择</option>
							<option value="0">会员</option>
							<option value="1">导购</option>
							<option value="2">讲师</option>
							<option value="3">经理</option>
							<option value="4">商家</option>
						</select>
					</div>
				</div>
				<div class="form-group">
					<label class="col-md-2 control-label">购买后身份:</label>
					<div class="col-md-10" style="width: 120px">
						<select class="form-control input-sm" name="buyAfter" id="buyAfter">
							<option value="">请选择</option>
							<option value="0">会员</option>
							<option value="1">导购</option>
							<option value="2">讲师</option>
							<option value="3">经理</option>
							<option value="4">商家</option>
						</select>
					</div>
				</div>
				<div class="form-group">
					<label class="col-md-2 control-label">状态:</label>
					<div class="col-md-10" style="width: 120px">
						<select class="form-control input-sm" name="status" id="status">
							<option value="0">正常</option>
							<option value="1">下架</option>
						</select>
					</div>
				</div>
				<div class="form-group">
					<label class="col-md-2 control-label">类型:</label>
					<div class="col-md-10" style="width: 120px">
						<select class="form-control input-sm" name="giftType"
							id="giftType">
							<option value="" selected="selected">请选择</option>
							<option value="1">399礼包</option>
							<option value="2">998礼包</option>
						</select>
					</div>
				</div>
				<div class="form-group">
					<label class="col-md-2 control-label">到期时间:</label>
					<div class="col-md-10">
						<input type="text" class="form-control" placeholder="到期时间"
							name="endTime" id="endTime" />
					</div>
				</div>
				<div class="form-actions">
					<div class="row" align="center">
						<div class="col-md-12">
							<button class="btn btn-primary"
								onclick="location.href='giftList.html'">返回</button>
							<button id="submitbtn" class="btn btn-primary" type="submit"
								onclick="add()">
								<i class="fa fa-save"></i> 保存
							</button>
						</div>
					</div>
				</div>
			</fieldset>
		</form>
	</div>

	<script type="text/javascript"
		src="../../global/js/libs/jquery-2.1.1.min.js"></script>
	<script type="text/javascript" src="../../global/js/jq.js"></script>
	<script type="text/javascript"
		src="../../global/js/plugin/bootstrapvalidator/bootstrapValidator.min.js"></script>
	<script type="text/javascript" src="../../global/js/common.js"></script>
	<script type="text/javascript" src="../../global/layui/layui.js"></script>
	<script type="text/javascript" src="../../global/js/ajaxfileupload.js"></script>
	<script type="text/javascript">
	layui.use(['layer', 'laydate'], function () {
        var layer = layui.layer;
        var laydate = layui.laydate;
        laydate.render({
            elem: '#endTime'
            ,type: 'datetime'
                ,format: 'yyyy-MM-dd HH:mm:ss'
        });
    });
		initData();
		function initData() {
			var id = getUrlParam("id");
//回显商品信息
			
			$.ajax({
				url:"/clyjGift/getGoods",
				type:"post",
				async : false,
				dataType:"json",
				success:function(data){
					if(data.code!="success")
						return;
					var goodsData = data.data;
					for(var i =0 ;i<goodsData.length;i++){
						$("#goodsName").append("<option id='"+goodsData[i].goodsDepict+"' value='"+goodsData[i].goodsId+"'>"+goodsData[i].goodsName+"</option>");
					}
					
				}
			})
			if (id != "") {
				$
						.ajax({
							type : 'get',
							url : '/clyjGift/' + id,
							async : false,
							success : function(data) {
								$("#id").val(data.id);
								$("#giftName").val(data.giftName);
								$("#zkPrice").val(data.zkPrice);
								$("#pocketMoney").val(data.pocketMoney);
								$("#status").val(data.status);
								$("#giftType").val(data.giftType);
								$("#buyBefor").val(data.buyBefor);
								$("#buyAfter").val(data.buyAfter);
								$("#goodsName").val(data.goodsName);
								goodsChange();
								var date = data.endTime
								if(date==null || date==""){
                                    return "";
                                }
                              var dateee = new Date(date).toJSON();
                              date=new Date(+new Date(dateee) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '')
                              $("#endTime").val(date);
								$("#picBanner1").attr("src", data.picBanner);
								//$("#picLb1").attr("src",data.picLb);
								$("#picBig1").attr("src", data.picBig);
								var picList = data.picLb;
								if (picList == null || picList == "")
									return;
								var picLb = picList.split(",");
								if (picLb != null && picLb.length > 0) {
									for (var j = 0; j < picLb.length; j++) {
										$("#picLb")
												.append(
														"<img src='"+picLb[j]+"'style='width:100px;height:60px' />")
									}
								}
							}
						});

			}
			
			
		}
		
		function goodsChange(){
			$("#goodsDetail").val($("#goodsName").find("option:selected").attr('id'));
		}

		$('#form').bootstrapValidator();

		//新增商品
		function add() {
			var bootstrapValidator = $("#form").data('bootstrapValidator');
			bootstrapValidator.validate();
			if (!bootstrapValidator.isValid()) {
				return;
			}
			$("form").ajaxSubmit({
				type : 'post',
				url : '/clyjGift/updategift',
				traditional : true,
				contentType : "application/json; charset=utf-8",
				success : function(data) {
					layer.msg("添加成功", {
						shift : -1,
						time : 1000
					}, function() {
						location.href = "giftList.html";
					});
				}
			})

		}
		
		//商品详情
		function goodsDeatil() {
			layer.open({
				  type: 1,
				  skin: 'layui-layer-rim', //加上边框
				  area: ['420px', '240px'], //宽高
				  content: $("#goodsDetail").val()
				});
		}
	</script>
</body>
</html>